<template>
	<xn-form-container
		:title="formData.wareId ? '编辑仓库' : '增加仓库'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="上级仓库：" name="parentId">

<!--						<a-select-->
<!--							v-model:value="formData.parentId"-->
<!--							show-search-->
<!--							placeholder="请输入上级仓库"-->
<!--							:options="topTypeData"-->
<!--							:fieldNames="{label: 'wareName', value: 'wareId'}"-->
<!--						></a-select>-->

						<a-tree-select
							v-model:value="formData.parentId"
							show-search
							style="width: 100%"
							:dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
							placeholder="请选择上级类型"
							allow-clear
							tree-default-expand-all
							:tree-data="topTypeData"
							:field-names="{
							  children: 'children',
							  label: 'wareName',
							  value: 'wareId',
							}"
							tree-node-filter-prop="wareName"
						></a-tree-select>

					</a-form-item>



				</a-col>
				<a-col :span="12">
					<a-form-item label="仓库名称：" name="wareName">
						<a-input v-model:value="formData.wareName" placeholder="请输入仓库名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="仓库负责人：" name="username">
						<a-input v-model:value="formData.username" placeholder="请输入仓库负责人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="负责人联系电话：" name="phone">
						<a-input v-model:value="formData.phone" placeholder="请输入负责人联系电话" allow-clear />
					</a-form-item>
				</a-col>


				<a-col :span="12">
					<a-form-item label="备注：" name="remark">
						<a-textarea v-model:value="formData.remark" placeholder="请输入备注" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>


				<a-col :span="12">
					<a-form-item label="状态：" name="status">
						<a-select v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
					</a-form-item>
				</a-col>

			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="imsWarehouseForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import imsWarehouseApi from '@/api/ims/imsWarehouseApi'
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({
		parentId: '1',
	})
	const submitLoading = ref(false)
	const statusOptions = ref([])
	const topTypeData = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		statusOptions.value = tool.dictList('COMMON_STATUS')
		formData.value.status = statusOptions.value[0].value


		// 上级分类
		imsWarehouseApi.wareTree().then(res => {
			topTypeData.value = res
		})
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value = {
			parentId: '1',
		}
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		wareName: [required('请输入仓库名称')],
		status: [required('请输入状态')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true
				const formDataParam = cloneDeep(formData.value)
				imsWarehouseApi
					.imsWarehouseSubmitForm(formDataParam, formDataParam.wareId)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
